function  disable(userId) {
    isDisable(userId,1,"确认要禁用么？")
}
function noDisable(userId) {
    isDisable(userId,0,"确认要解禁么？")
}

function isDisable(userId,status,msg){
    var data={
        userId:userId,
        status:status
    }
    confirm(msg, function(){
        $.ajax({
            type: "POST",
            url: baseURL + "user/user/status",
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function(r){
                vm.reload()
                console.log(r)
                alert("操作成功")
            }
        });
    });
}


$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'user/user/list',
        datatype: "json",
        colModel: [
			{ label: 'userId', name: 'userId', index: 'user_id', width: 50, key: true },
            { label: '昵称', name: 'nickName', index: 'nick_name', width: 80 },
            { label: '头像', name: 'headImg', index: 'head_img', width: 80, formatter: function(value, options, row){
					if(value!=null)
                     	return   `<img src="${value}" style="height:80px;width: 80px "   height:"40px" />`;
					else
						return ``;
                } },
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 80 },
			{ label: '注册时间', name: 'createTime', index: 'create_time', width: 80 },
			{ label: 'QQ号', name: 'qqNumber', index: 'qq_number', width: 80 }, 			
			{ label: '性别', name: 'sex', index: 'sex', width: 80 }, 			
			{ label: '年龄', name: 'age', index: 'age', width: 80 },
            { label: '学校名称', name: 'schoolName', index: 'school_name', width: 80 },
            // { label: '是否绑定QQ', name: 'isbindingmobile', index: 'isBindingMobile', width: 80 },
			{ label: '地址', name: 'address', index: 'address', width: 80 },
			{ label: '积分', name: 'integral', index: 'integral', width: 80 }, 			
			{ label: '是否认证，0未认证。1已认证', name: 'isAuthentication', index: 'is_authentication', width: 80 , formatter: function(value, options, row){
                    if(value==0)
                        return   `<span style="color: #ffd20f">未认证</span>`;
                    else if(value==1)
                        return `<span style="color: #00a65a">已认证</span>`;
                } },
			{ label: '绑定QQ', name: 'isBangDingQQ', index: 'is_bang_ding_q_q', width: 80, formatter: function(value, options, row){
                    if(value==0)
                        return   `<span style="color: #ffd20f">未绑定</span>`;
                    else if(value==1)
                        return `<span style="color: #00a65a">已绑定</span>`;
                }  },
			{ label: '绑定微信', name: 'isBangDingWeChat', index: 'is_bang_ding_we_chat', width: 80, formatter: function(value, options, row){
                    if(value==0)
                        return   `<span style="color: #ffd20f">未绑定</span>`;
                    else if(value==1)
                        return `<span style="color: #00a65a">已绑定</span>`;
                }  },
			{ label: '关注', name: 'attentionCount', index: 'attention_count', width: 80 },
			{ label: '粉丝', name: 'fansCount', index: 'fans_count', width: 80 },
			{ label: '笔记', name: 'noteCount', index: 'note_count', width: 80 },
			{ label: '点赞和收藏', name: 'likeCount', index: 'like_count', width: 80 },
            { label: '操作', name: 'status', index: '', width: 80, formatter: function(value, options, row){
                    var html;
                    if(value==0){
                        html=` <button class="btn btn-danger" onclick="disable(${row.userId})"></i>&nbsp;禁用</button>`
                    }else {
                        html=` <button class="btn btn-success"onclick="noDisable(${row.userId})"></i>&nbsp;解禁</button>`
                    }
                    return html;
                }  },

        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		user: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.user = {};
		},
		update: function (event) {
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(userId)
		},
		saveOrUpdate: function (event) {
			var url = vm.user.userId == null ? "user/user/save" : "user/user/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.user),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "user/user/delete",
                    contentType: "application/json",
				    data: JSON.stringify(userIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(userId){
			$.get(baseURL + "user/user/info/"+userId, function(r){
                vm.user = r.user;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});